Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in Async Example: ReferenceError: regeneratorRuntime is not defined #3126

Closed
mpontus opened this issue Mar 12, 2017 · 22 comments
Closed

Comments

@mpontus
Copy link
Contributor

mpontus commented Mar 12, 2017

I'm getting following error when trying to run an example from this directory:
https://github.com/facebook/jest/tree/master/examples/async

 FAIL  __tests__/user-test.js
  ● Test suite failed to run

    ReferenceError: regeneratorRuntime is not defined

      at Object.<anonymous> (__tests__/user-test.js:16:48)
      at process._tickCallback (internal/process/next_tick.js:103:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.495s
Ran all test suites.

Same issue can be reproduced using node 6.10 and node 7.7.4:

git clone https://github.com/facebook/jest jest
cd jest/examples/async
npm install
npm run test
@cpojer
Copy link
Member

cpojer commented Mar 13, 2017

This doesn't repro locally on our CI or locally. Maybe there is something wrong with your version of npm?

@cpojer cpojer closed this as completed Mar 13, 2017
@mpontus
Copy link
Contributor Author

mpontus commented Mar 13, 2017

@cpojer

Thanks Chris.

This issue can be reproduced from docker container running node:latest from following repository:
https://hub.docker.com/_/node/

Below are the steps which lead to an error:

# from local machine (also tested in vagrant 1.9.2 running ubuntu/trusty64)
docker pull node:latest
docker run --rm -it node bash
# from inside the container (docker version 17.03.0-ce, node version 7.7.2)
git clone https://github.com/facebook/jest /jest
cd /jest
# as per instructions from https://github.com/facebook/jest/tree/master/examples
npm install
cd examples/async
npm install
npm test

Am I correct in the way I execute this example?
Is it possible that I'm missing some global requirements that will make this scenario pass?
Can I offer you any additional info?

Edit:
I found that example works if you replace regeneration-runtime with babel-polyfill in devDependencies.

I'm going to assume that jest in master is actually compatible with the former module, but the example doesn't use master code and instead pulls the latest release which may still require babel-polyfill.

@okonet
Copy link
Contributor

okonet commented Mar 17, 2017

I'm experiencing the same issue with Travis CI: https://travis-ci.org/okonet/lint-staged/jobs/212179781

Tests running okay locally but not on CI

@thymikee
Copy link
Collaborator

I just bumped into this issue!

@mpontus @okonet you can use jest@next where it's fixed or otherwise you need install babel-polyfill.

Jest used to autoinclude babel-polyfill (and still does it in 19.0.2), but since it caused memory leaks, we moved to using regenerator-runtime only, which ships with our transitive deps (so since Jest v20 if you're using npm >=3 or yarn you don't need to install anything for async/await support).

The problem is that we eradicated babel-polyfill from docs, but forgot to include this diff #2755 in 19.0.2 and didn't roll out a decent release since then (only jest@next tag).
Sorry about the inconvenience!

@leimonio
Copy link

@thymikee How can I use regenerator-runtime as described in the docs along with a transform?
I'm not using any .babelrc file in my project and load a babel configuration which is shared for both webpack and jest configuration just like below:

jest.config.js

...
'transform': {
    '^.+\\.js$': '<rootDir>/config/jest.transform.js'
}
...

jest.transform.js

var babelConfig = require('./babel.config')

module.exports = require('babel-jest').createTransformer(babelConfig)

@tmcw
Copy link

tmcw commented Aug 11, 2017

Sorry to wake up an old issue: I'm running into the same issue with Jest 20.0.4, and can't really suss out the take-away of these comments. From what I can see:

  • The async/await documentation advises to use babel-preset-env, but the async/await example uses babel-plugin-transform-runtime instead. Which is the recommended dependency?
  • Like okonet, the regeneratorRuntime crops up on CircleCI but not locally. Running my CI test script locally with --runInBand does not indicate that that's the issue.
  • It looks like the root cause is that regenerator-runtime needs to be installed directly if you're using an older package manager, which, on CI, is likely.

@AvraamMavridis
Copy link
Contributor

I was facing the same issue, importing the babel-polyfill directly into the jest.init.js (jest setup file) solved the issue for me.

  ...
 "setupFiles": [
   "<rootDir>/jest.init.js"
  ],
  ...
/// jest.init.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'babel-polyfill';

@ConAntonakos
Copy link

ConAntonakos commented Dec 19, 2017

Sorry for the bump! But I wanted to share 😄 . Adding this in my general plugins works for me:

    [
      "transform-runtime",
      {
        "helpers": false,
        "polyfill": false,
        "regenerator": true
      }
    ]

I don't know if this is the best way of handling it, though.

@ghengeveld
Copy link

This worked for me to fix "ReferenceError: regeneratorRuntime is not defined" in Jest:

npm install --save-dev @babel/plugin-transform-runtime

Then in .babelrc (besides other options):

{
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-runtime"]
    }
  }
}

ragesoss added a commit to WikiEducationFoundation/WikiEduDashboard that referenced this issue Mar 6, 2019
To avoid errors with using `async` in the jest tests on v24, this fix was required: jestjs/jest#3126 (comment)
@PierreTurnbull
Copy link

PierreTurnbull commented Apr 5, 2019

I was facing the same issue, importing the babel-polyfill directly into the jest.init.js (jest setup file) solved the issue for me.

  ...
 "setupFiles": [
   "<rootDir>/jest.init.js"
  ],
  ...
/// jest.init.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'babel-polyfill';

I tried importing the polyfill in jest.init.js but it broke my tests weirdly. I didn't have the undefined generator error but I had lots of errors that I didn't have previously in my tests, such as missing component imports or missing props.

My jest.conf.js contained the following line:

setupFiles: ['<rootDir>/test/unit/setup']

Hence I had to import the polyfill in setup.js instead of jest.init.js (which did not exist):

import Vue from 'vue'
import '@babel/polyfill';

Vue.config.productionTip = false
Vue.config.silent = true

Except for the polyfill import line, the 3 other lines were already in the file when I opened it.

Importing the polyfill in setup.js worked while importing it in jest.init.js didn't work (I changed the setupFiles path).

@orpheus
Copy link

orpheus commented Apr 15, 2019

Adding targets node current worked for me to fix "ReferenceError: regeneratorRuntime is not defined" in Jest:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

@dannypk
Copy link

dannypk commented Aug 9, 2019

Adding targets node current worked for me to fix "ReferenceError: regeneratorRuntime is not defined" in Jest:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

This won't actually help babelyfing, my code won't be compiled with the target node current. It will look exactly the same (ES6). Tests would be running, great, but no compiled code :P

@reduxdj
Copy link

reduxdj commented Aug 9, 2019

Here's a snippit of my package.json, this works for me now.

"babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ],
      "@babel/preset-react"
    ],
    "plugins": [
      "component-identification",
      "@babel/plugin-proposal-class-properties",
      [
        "module-resolver",
        {
          "root": [
            "./src"
          ]
        }
      ]
    ]
  },

@AoDev
Copy link

AoDev commented Aug 12, 2019

For me it was not enough to have preset-env targets specified in common config.
I had to define the targets again explicitly for node under env.test.

{
  'presets': [
    [
      '@babel/preset-env',
      {
        'targets': {
          'chrome': 61,
          'node': 8,
          'electron': '2.0.9'
        },
      }
    ],
    '@babel/typescript',
    '@babel/preset-react'
  ],
  'env': {
    'test': {
      'presets': [
        [
          '@babel/preset-env',
          {
            'targets': {
              'node': 8,  // <- ADDED
            },
          }
        ],
        '@babel/typescript',
      ]
    }
  }
}

@jktravis
Copy link

The only thing I had to do is to define the target in babel as mentioned by @AoDev. No additional targets elsewhere.

@odinho
Copy link

odinho commented Aug 15, 2019

To import and define regenerator-runtime globally, you do:

require('regenerator-runtime/runtime');

For those that just want to add the required thing, and not mess around with adding all babel polyfills for jest.

@SimenB
Copy link
Member

SimenB commented Aug 15, 2019

This was mentioned in the blog post introducing Jest 24, if people missed it: https://jestjs.io/blog/2019/01/25/jest-24-refreshing-polished-typescript-friendly#breaking-changes. You need to configure babel properly, no need to change any code. See this comment from the PR: #7595 (comment)

@behnammodi
Copy link

@ghengeveld This is work for me, Thank you

@WidedKaper
Copy link

WidedKaper commented Jan 11, 2020

What solved it for me, was to not just add the regenerator, but also the runtime to my config:

  'globals': {
    'vue-jest': {
      'babelConfig': {
        'plugins': [
          '@babel/plugin-transform-regenerator',
          '@babel/plugin-transform-runtime',
        ],
        'presets': [['@babel/preset-env', { 'modules': false }]],
        'env': {
          'test': {
            'presets': [
              ['@babel/preset-env', { 'targets': { 'node': 'current' } }],
            ],
          },
        },
      },
    },
  },

@muraliseveneleven
Copy link

muraliseveneleven commented May 21, 2020

@here none of the attempts here have worked out for me , I still seem to be getting Test suite failed to run

ReferenceError: regeneratorRuntime is not defined

This my my package.json
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/plugin-transform-regenerator": "^7.8.7",
"@babel/runtime": "^7.9.6",
"@react-native-community/eslint-config": "^0.0.5",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.9.0",
"babel-plugin-module-resolver": "^3.1.3",
"babel-plugin-transform-runtime": "^6.23.0",
"detox": "^16.5.1",

.babelrc
whenever I add something on this file , my app files will not run. I have to delete this file back to have my reactive native app running

vnys referenced this issue in equinor/design-system Jun 23, 2020
* First commit

* Simple version of snackbar

* Add support for async/await in tests https://github.com/facebook/jest/issues/3126\#issuecomment-483320742

* Tests for snackbar

* Add better button labels, add knobs for storybook

* Add version with Moss green 34 as action button color

* Remove button color because of lack of contrast on hover

* Wider query for 'larger screen' -> 1200px. Swap center and left, had this mixed up

* Add lighter button color example
shalin1 added a commit to shalin1/frontend-takehome-parcel that referenced this issue Jul 25, 2020
Also added babel-polyfill to setup, which is no longer
automatically included with jest.

See jestjs/jest#3126 (comment)
@tot-ra
Copy link

tot-ra commented Nov 9, 2020

Encountered same issue. Fresh jest (26.6.3), plain async test and this..
For a framework that defines itself as "delightful" this really is not.
Why on earth do I need to bother about babel configuration, plugins etc.
Why can't it work out of the box?

upd. In my case, I had babel.config.js already but was missing
targets: { node: 'current' }

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests